import json
import os

from langchain import requests
from langchain.agents import initialize_agent, AgentType
from langchain.chains.retrieval_qa.base import RetrievalQA
from langchain.memory import ConversationBufferMemory
from langchain_chroma import Chroma
from langchain_community.document_loaders import PyPDFLoader, PyMuPDFLoader
from langchain_community.embeddings import DashScopeEmbeddings
from langchain_core import memory
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder, StringPromptTemplate
from langchain_core.runnables import RunnablePassthrough, RunnableLambda
from langchain_core.tools import Tool, BaseTool
from langchain_openai import ChatOpenAI
from langchain_text_splitters import RecursiveCharacterTextSplitter
from langchain_core.tools import BaseTool
from langgraph.constants import START, END
from langgraph.graph import StateGraph
from langgraph.prebuilt import ToolNode
from pydantic import BaseModel, Field, PrivateAttr
from typing import Optional, Type, List
import requests

os.environ['LANGCHAIN_TRACING_V2'] = 'true'
os.environ['LANGCHAIN_PROJECT'] = 'LLMDEMO'
os.environ['LANGCHAIN_API_KEY'] = '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'
def my_node(state):
    return {"x":state["x"] + 1, "y":state["y"] + 1}

stat = StateGraph(dict)
tools = [
    Tool(
        name="my_node",
        func=my_node,
        description="这是一个简单的工具，输入一个字典，输出一个字典",
    )
]
tool_node = ToolNode(tools)
# print(
# .invoke("成都今天天气怎样？"))
#模型绑定工具
stat.add_node(my_node)
stat.add_edge(START, "my_node")
stat.add_conditional_edges("my_node", "tools")
stat.add_edge("tools", "my_node")
stat.add_edge("my_node", END)
# stat.add_edge("tools", END)
graph = stat.compile()
graph_png = graph.get_graph().draw_mermaid_png()
with open("graph.png", "wb") as f:
    f.write(graph_png)
#下载图片
print(graph)